# -*- coding: utf-8 -*-
# Project : flacon
# File : project.py
# (c) Alexander Sokolov sokoloff.a@gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.


from settings import *
from flaconglobal import *
from disc import *

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *


class Project(QObject):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, parent = None):
        QObject.__init__(self, parent)
        self._discs = []


   #****************************************************************
   #
   #****************************************************************
    def __getitem__(self, index):
        return self._discs[index]


   #****************************************************************
   #
   #****************************************************************
    def count(self):
        return len(self._discs)


   #****************************************************************
   #
   #****************************************************************
    def indexOf(self, disc):
        try:
            return self._discs.index(disc)
        except:
            return -1


   #****************************************************************
   #
   #****************************************************************
    def trackDataChanged(self, disc, trackNum):
        n = self._discs.index(disc)
        self.emit(SIGNAL("trackDataChanged(int,int)"), n, trackNum)


   #****************************************************************
   #
   #****************************************************************
    def layoutChanged(self):
        self.emit(SIGNAL("layoutChanged()"))


   #****************************************************************
   #
   #****************************************************************
    def insertDisc(self, disc, index=-1):
        if index < 0:
            index = len(self._discs)

        self._discs.insert(index, disc)
        disc.setProject(self)
        self.connect(disc, SIGNAL("downloadStarted()"), self._discDownlaodStarted)
        self.connect(disc, SIGNAL("downloadFinished()"), self._discDownlaodFinished)
        self.emit(SIGNAL("layoutChanged()"))

        return index


   #****************************************************************
   #
   #****************************************************************
    def removeDisc(self, disc):
        n = self.indexOf(disc)
        self.emit(SIGNAL("beginRemoveDisc(int)"), n)
        res = self._discs.remove(disc)
        self.emit(SIGNAL("endRemoveDisc()"))

        return res


   #****************************************************************
   #
   #****************************************************************
    def _discDownlaodStarted(self):
        self.emit(SIGNAL("downloadStarted(int)"), self.indexOf(self.sender()))


   #****************************************************************
   #
   #****************************************************************
    def _discDownlaodFinished(self):
        self.emit(SIGNAL("downloadFinished(int)"), self.indexOf(self.sender()))


   #****************************************************************
   #
   #****************************************************************
    def canConvert(self):
        for disc in self._discs:
            if disc.canConvert():
                return True

        return False


